#coding=utf-8
#Boa:Dialog:DlgProductImage

import wx

from product import CDataCategory, CDataCustomer, CDataProduct
import os

def create(parent):
    return DlgProductImage(parent)

[wxID_DLGPRODUCTIMAGE, wxID_DLGPRODUCTIMAGEBTNCHECKCUSTOMER, 
 wxID_DLGPRODUCTIMAGEBUTTONDIR, wxID_DLGPRODUCTIMAGEBUTTON_NEXT, 
 wxID_DLGPRODUCTIMAGEBUTTON_PRE, wxID_DLGPRODUCTIMAGESTATICBITMAP1, 
 wxID_DLGPRODUCTIMAGESTATICTEXT_INDEX, 
] = [wx.NewId() for _init_ctrls in range(7)]

class DlgProductImage(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPRODUCTIMAGE,
              name=u'DLGPRODUCTIMAGE', parent=prnt, pos=wx.Point(728, 246),
              size=wx.Size(985, 668), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5546\u54c1\u56fe\u7247')
        self.SetClientSize(wx.Size(969, 630))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Center(wx.BOTH)

        self.button_pre = wx.Button(id=wxID_DLGPRODUCTIMAGEBUTTON_PRE,
              label=u'\u4e0a\u4e00\u5f20', name=u'button_pre', parent=self,
              pos=wx.Point(32, 16), size=wx.Size(93, 26), style=0)
        self.button_pre.Bind(wx.EVT_BUTTON, self.OnButton_preButton,
              id=wxID_DLGPRODUCTIMAGEBUTTON_PRE)

        self.button_next = wx.Button(id=wxID_DLGPRODUCTIMAGEBUTTON_NEXT,
              label=u'\u4e0b\u4e00\u5f20', name=u'button_next', parent=self,
              pos=wx.Point(200, 16), size=wx.Size(93, 26), style=0)
        self.button_next.Bind(wx.EVT_BUTTON, self.OnButton_nextButton,
              id=wxID_DLGPRODUCTIMAGEBUTTON_NEXT)

        self.staticText_index = wx.StaticText(id=wxID_DLGPRODUCTIMAGESTATICTEXT_INDEX,
              label=u'1', name=u'staticText_index', parent=self,
              pos=wx.Point(160, 24), size=wx.Size(9, 16), style=0)

        self.staticBitmap1 = wx.StaticBitmap(bitmap=wx.NullBitmap,
              id=wxID_DLGPRODUCTIMAGESTATICBITMAP1, name='staticBitmap1',
              parent=self, pos=wx.Point(76, 81), size=wx.Size(600, 600),
              style=0)

        self.buttonDir = wx.Button(id=wxID_DLGPRODUCTIMAGEBUTTONDIR,
              label=u'\u6253\u5f00\u76ee\u5f55', name=u'buttonDir', parent=self,
              pos=wx.Point(360, 16), size=wx.Size(93, 26), style=0)
        self.buttonDir.Bind(wx.EVT_BUTTON, self.OnButtonDirButton,
              id=wxID_DLGPRODUCTIMAGEBUTTONDIR)

        self.btnCheckCustomer = wx.Button(id=wxID_DLGPRODUCTIMAGEBTNCHECKCUSTOMER,
              label=u'\u68c0\u67e5\u5ba2\u6237\u56fe\u7247',
              name=u'btnCheckCustomer', parent=self, pos=wx.Point(832, 16),
              size=wx.Size(117, 26), style=0)
        self.btnCheckCustomer.Bind(wx.EVT_BUTTON, self.OnBtnCheckCustomerButton,
              id=wxID_DLGPRODUCTIMAGEBTNCHECKCUSTOMER)

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.btnCheckCustomer.Show(False)
    
        if (DlgProductImage.image_dir is None):
            if (os.path.exists("image")):
                DlgProductImage.image_dir = "image"
            else:
                # if (os.path.exists("Z:")):
                    # os.system('net use Z: /delete')
                    
                # try:
                    # os.system('net use Z: \\DESKTOP-NOG446T\image "lmy0602" /user:"linda"')     
                # except:
                    # pass
                os.system('netuse.bat')    
                DlgProductImage.image_dir = "Z:"
        
    image_dir = None
    def SetProduct(self, proId):
        self.imageList = []
        self.imageIdx = 0
        
        try:
            proObj = CDataProduct.getData({'id':proId})
            self.category = proObj.get_category_name()
            self.dir = '%s/product/%s-%s' %(DlgProductImage.image_dir, proObj.getFieldVal(index=proObj.COL_CATEGORYID), self.category)
            self.imageList = [x for x in os.listdir(self.dir) if x.startswith('%d-'%proId) or x.startswith('common-') or x.startswith('%s-'%proObj.getValue(proObj.COL_IMGPREFIX))]
            self.ChangeBitmap()
        except:
            dlg = wx.MessageDialog(self, u'查找图片失败', u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            self.Destroy()#Close()
    
    def SetCustomer(self, peerId):
        self.dir = '%s/customer' %(DlgProductImage.image_dir)
        self.imageList = [x for x in os.listdir(self.dir) if x.startswith('%d-'%peerId)]
        self.imageIdx = 0
        self.ChangeBitmap()
    
    def ChangeBitmap(self):
        
        if (len(self.imageList) <= 0):
            dlg = wx.MessageDialog(self, u'没有找到图片', u'提示',wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            self.Destroy()
            return
        
        self.staticText_index.SetLabel('%d/%d' %(self.imageIdx+1, len(self.imageList)))
        
        image = wx.Image('%s/%s' %(self.dir, self.imageList[self.imageIdx]), wx.BITMAP_TYPE_ANY)
        if (image.GetWidth() > image.GetHeight()):
            width = 600
            height = image.GetHeight() * 600 / image.GetWidth()
        else:
            height = 600
            width = image.GetWidth() * 600 / image.GetHeight()
            
        #self.staticBitmap1.Move(wx.Point(760, 200))
        self.staticBitmap1.SetSize(wx.Size(width,height))
        self.staticBitmap1.SetBitmap(image.Scale(width,height).ConvertToBitmap())
        
        
    def OnButton_preButton(self, event):
        self.imageIdx = self.imageIdx - 1 if self.imageIdx - 1 > 0 else len(self.imageList) - 1
        self.ChangeBitmap()

    def OnButton_nextButton(self, event):
        self.imageIdx = (self.imageIdx + 1) % len(self.imageList)
        self.ChangeBitmap()

    def OnButtonDirButton(self, event):
        import os
        import sys
        reload(sys)
        sys.setdefaultencoding('utf-8')
        print ("explorer /select,%s/%s" %(self.dir, self.imageList[self.imageIdx]))
        #os.system(r"explorer /select,%s/%s" %(self.dir, self.imageList[self.imageIdx]))
        #os.system(r"explorer %s" %(self.dir))
        os.startfile(self.dir)

    def OnBtnCheckCustomerButton(self, event):
        ''' 检查客户图片数量，并补充到客户备注 [0p]，以便提示文员补充图片 '''
        
        dir = '%s/customer' %(DlgProductImage.image_dir)        
        for obj in CDataCustomer.getObjList():
            imageList = [x for x in os.listdir(dir) if x.startswith('%d-'%(obj.getKeyVal()))]
            obj.updateMemoPic(len(imageList))

